/* 
* Copyright (C) 2011-2018 ShenZhen iBOXSaaS Information Technology Co.,Ltd. 
* 
* All right reserved. 
* 
* This software is the confidential and proprietary 
* information of iBOXSaaS Company of China. 
* ("Confidential Information"). You shall not disclose 
* such Confidential Information and shall use it only 
* in accordance with the terms of the contract agreement 
* you entered into with iBOXSaaS inc. 
* 
*/

package org.iboxpay.open.common.token;

import java.util.Date;

import org.iboxpay.open.common.constants.ResultEnums;

import io.jsonwebtoken.Claims;

/**
 * 
 * The class VerifyResult.
 *
 * Description: token验证结果返回信息
 *
 * @author: qulei
 * @since: 2018年3月29日
 * @version: $Revision$ $Date$ $LastChangedBy$
 * @param <T>
 *
 */
public class VerifyResult<T> {

	/**
	 * 接口返回结果（业务处理成功：1）   
	 */
	private ResultEnums resultCode;

	/**
	 * 错误描述
	 */
	private String errorDesc;

	/**
	 * 返回结果
	 */
	private T result;

	public ResultEnums getResultCode() {
		return resultCode;
	}

	public void setResultCode(ResultEnums resultCode) {
		this.resultCode = resultCode;
	}

	public String getErrorDesc() {
		return errorDesc;
	}

	public void setErrorDesc(String errorDesc) {
		this.errorDesc = errorDesc;
	}

	public T getResult() {
		return result;
	}

	public void setResult(T result) {
		this.result = result;
	}

	@Override
	public String toString() {
		return "VerifyResult [resultCode=" + resultCode + ", errorDesc=" + errorDesc + ", result=" + result + "]";
	}

	/**
	 * 校验返回结果，token是否在有效期内
	 * @param result
	 * @return
	 */
	public boolean isVaild() {
		if (this.resultCode == ResultEnums.success) {
			return true;
		}
		return false;
	}

	/**
	 * 校验返回结果，是否需要刷新token
	 * @param result
	 * @return
	 */
	public boolean isNeedFresh() {
		if (this.resultCode == ResultEnums.success) {
			if (null != this.result) {
				Claims claims = (Claims) result;
				Date expiration = claims.getExpiration();
				// 失效时间
				long expirationTime = expiration.getTime();
				// 当前时间
				long currentTime = System.currentTimeMillis();
				// 时间间隔
				double interval = (expirationTime - currentTime) / (1000 * 60);
				// 如果距离失效时间还有一分钟，提示刷新
				if (interval < 1 && interval > 0) {
					return true;
				}
			}
		}
		return false;
	}
}
